package com.cy.module;

import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.CompanyCategory;
import com.jfinal.plugin.activerecord.Page;

@ControllerBind(controllerKey = "/clmp/companyCategory")
public class CompanyCategoryController extends WebApiController {

    @Override
    public void index() {
        CompanyCategory companyCategory = getModel(CompanyCategory.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Page<CompanyCategory> companyCategoryPage = CompanyCategory.dao.paginate(page, pageSize, CompanyCategory.SELECT_ALL, companyCategory);
        renderBuiPage(companyCategoryPage, 200);
    }

    @Override
    public void update() {
        int id = getParaToInt();
        CompanyCategory tempCompanyCategory = getModel(CompanyCategory.class);
        CompanyCategory companyCategory = CompanyCategory.dao.findById(id);
        companyCategory.cover(tempCompanyCategory);
        try {
            companyCategory.update();
            renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            renderFail();
        }
    }

    @Override
    public void save() {
        CompanyCategory companyCategory = getModel(CompanyCategory.class);
        try {
            companyCategory.save();
            renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            renderFail();
        }
    }

    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            CompanyCategory.dao.deleteById(id);
        }
        renderSuccess();
    }
}